﻿@using Masa.Blazor.PullToRefresh
@using StyleBuilder = Masa.Blazor.Core.StyleBuilder

@namespace Masa.Blazor
@inherits MasaComponentBase
@inject I18n I18n

<div class="@GetClass()"
     style="@GetStyle()"
     id="@Id"
     @ref="Ref">
    <div class="@_block.Element("track")"
         style="@(StyleBuilder.Create().Add("transition-duration", $"{_duration}ms").AddIf("transform", $"translate3d(0, {_distance}px, 0)", _distance > 0).Build())"
         @ref="@_trackRef"
         @ontouchend="OnTouchEnd"
         @ontouchcancel="OnTouchEnd">
        <div class="@_block.Element("header")"
             style="@CssStyleUtils.GetHeight(HeadHeight)">
            @RenderLoadingStatus()
            @RenderCanReleaseStatus()
            @RenderPullingStatus()
            @RenderSuccessStatus()
        </div>

        @ChildContent
    </div>
</div>

@code {

    private RenderFragment RenderLoadingStatus() => __builder =>
    {
        if (_pullRefreshStatus == PullRefreshStatus.Loading)
        {
            if (LoadingContent is null)
            {
                <MProgressCircular Indeterminate Size="24" Width="2" />
            }
            else
            {
                @LoadingContent(_distance);
            }
        }
    };

    private RenderFragment RenderCanReleaseStatus() => __builder =>
    {
        if (_pullRefreshStatus == PullRefreshStatus.CanRelease)
        {
            if (CanReleaseContent is null)
            {
                @CanReleaseText
            }
            else
            {
                @CanReleaseContent(_distance);
            }
        }
    };

    private RenderFragment RenderPullingStatus() => __builder =>
    {
        if (_pullRefreshStatus == PullRefreshStatus.Pulling)
        {
            if (PullingContent is null)
            {
                @PullingText
            }
            else
            {
                @PullingContent(_distance);
            }
        }
    };

    private RenderFragment RenderSuccessStatus() => __builder =>
    {
        if (_pullRefreshStatus == PullRefreshStatus.Success)
        {
            if (SuccessContent is null)
            {
                @SuccessText
            }
            else
            {
                @SuccessContent
            }
        }
    };

}
